package com.example.homework_weather

import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.homework_weather.R
import com.example.homework_weather.Weather.Weather
import com.example.homework_weather.Weather.Forecast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

class MainActivity2 : AppCompatActivity() {
    private val baseURL = "http://t.weather.itboy.net/api/weather/city/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val cityCode = intent.getStringExtra("cityCode")
        val queue = Volley.newRequestQueue(this)

        val textView_city: TextView = findViewById(R.id.textView_city)
        val textView_province: TextView = findViewById(R.id.textView_province)
        val textView_shidu: TextView = findViewById(R.id.textView_shidu)
        val textView_wendu: TextView = findViewById(R.id.textView_wendu)
        val imageView: ImageView = findViewById(R.id.imageView)
        val listView: ListView = findViewById(R.id.listView2)

        val stringRequest = StringRequest(baseURL + cityCode, { response ->
            Log.d("MainActivity2", "Response: $response")
            val gson = Gson()
            val WeatherType = object : TypeToken<Weather>() {}.type
            val weather = gson.fromJson<Weather>(response, WeatherType)

            if (weather != null) {
                textView_city.text = weather.cityInfo.city
                textView_province.text = weather.cityInfo.parent
                textView_shidu.text = weather.data.shidu
                textView_wendu.text = weather.data.wendu

                val firstDay = weather.data.forecast.firstOrNull()
                if (firstDay != null) {
                    when (firstDay.type) {
                        "晴" -> imageView.setImageResource(R.drawable.sun)
                        "多云" -> imageView.setImageResource(R.drawable.mcloud)
                        "阵雨" -> imageView.setImageResource(R.drawable.rain)
                        "阴" -> imageView.setImageResource(R.drawable.cloud)
                        else -> imageView.setImageResource(R.drawable.thunder)
                    }
                }

                val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, weather.data.forecast)
                listView.adapter = adapter

                Log.d("MainActivity2", "${weather.cityInfo.city} ${weather.cityInfo.parent}")
            } else {
                Log.e("MainActivity2", "Weather data is null")
            }
        }, { error ->
            Log.e("MainActivity2", "Error: ${error.toString()}")
            Toast.makeText(this, "Network error: Unable to fetch weather data", Toast.LENGTH_LONG).show()
        })

        queue.add(stringRequest)
    }
}